//
//  GRMyOwnViewController.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/8/29.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRMyOwnViewController.h"
#import "GRMyOwnHeaderReusableView.h"
#import "GRPersonFunctionReusableView.h"
#import "GROwnCenterCell.h"
#import "GRUserConfigure.h"
#import "GRPersonModel.h"

#import "GRHaveBeenShopViewController.h"
#import "GRMyPaidOrderViewController.h"
#import "GRMyCollectionViewController.h"
#import "GRSystemSettingViewController.h"
#import "GROrderManagerListViewController.h"
#import "GRRiceOrderListVC.h"
#import "GRCouponsTableViewController.h"
#import "GRMyWalletViewController.h"
#import "GRWithdrawalViewController.h"
#import "GRIDCardCertificationViewController.h"
#import "GRInviteFriendViewController.h"
#import "GRMyInfomationViewController.h"
#import "GRCertifictionView.h"
#import "GRBasisKeyModel.h"
#import "GRMyGRiceViewController.h"
@interface GRMyOwnViewController () <UITableViewDelegate, UITableViewDataSource,GRCertifictionViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightLayout;

@property (weak, nonatomic) IBOutlet UIView *topNavigationBar;

@property (weak, nonatomic) IBOutlet UITableView *ownTableView;
@property (strong, nonatomic) IBOutlet UIView *headerView;

@property(nonatomic,strong)NSArray * dataSourceArr;

@property (nonatomic, strong) GRPersonModel *personModel;
@property (weak, nonatomic) IBOutlet UIView *BKView;

@property (weak, nonatomic) IBOutlet UIButton *loginBt;

@property (weak, nonatomic) IBOutlet UIButton *certificationBt;

@property (weak, nonatomic) IBOutlet UIImageView *avtarIamgeView;

@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *pointLabel;
@property (weak, nonatomic) IBOutlet UIView *recommendView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftOffset;
@property(nonatomic,strong)GRCertifictionView * certificationView;

@end

@implementation GRMyOwnViewController

-(GRCertifictionView *)certificationView{
    if (!_certificationView) {
        _certificationView = [GRCertifictionView xx_instantiateFromNib];
        _certificationView.delegate = self;
    }
    return _certificationView;
}
-(NSArray *)dataSourceArr{
    if(!_dataSourceArr){
        _dataSourceArr = @[@"优惠券",@"我的买单",@"联系我们"];
    }
    return _dataSourceArr;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if ([GRUserBasisInfo isLogin]) {
        [self getInfomation];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

/**
 消息中心
 */
- (IBAction)msgCenter:(id)sender {
    [[GRUMengCommon shareUMeng] enterAppletChat:self];
}

/**
 设置
 */
- (IBAction)userSetting:(id)sender {
    [self haveLoginOperation:^{
        GRSystemSettingViewController *system = [[GRSystemSettingViewController alloc] initWithStyle:UITableViewStylePlain];
        [self.navigationController pushViewController:system animated:YES];
    }];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    self.BKView.layer.shadowOffset = CGSizeMake(2, 2);
//    self.BKView.layer.shadowColor = [HEXCOLORA(0x000000, 0.16) CGColor];
//    self.BKView.layer.shadowOpacity = 0.9;
    [self.loginBt.layer setBorderWidth:1];
    [self.loginBt.layer setBorderColor:[HEXCOLOR(0xEEEEEE) CGColor]];
    [self dataProcess];
    [self.ownTableView registerNib:[UINib nibWithNibName:@"GROwnCenterCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ownCell"];
    self.ownTableView.tableHeaderView = self.headerView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doLoginOrLogout:) name:LoginToExitNotificationName object:nil];
    
}

// collectionView 配置
- (void)dataProcess {
    if (IS_IPHONEX) {
        _heightLayout.constant = 88;
    }
 
    
    self.ownTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getInfomation];
    }];
    
    

}

/**
 获取用户信息
 */
- (void)getInfomation {
    WEAKSELF
    [GRPersonAction getMemberShipInfomation:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            weakSelf.personModel                     = [GRPersonModel yy_modelWithDictionary:responseObject];
            [self doLoginOrLogout:nil];

        }
        [weakSelf.ownTableView.mj_header endRefreshing];
    }];
}

/**
 登录或退出登录
  */
- (void)doLoginOrLogout:(NSNotification *)notification {
    //刷新头视图
    if([GRUserBasisInfo isLogin]){
        self.userName.text = self.personModel.nickname;
        [self.loginBt setTitle:@"个人信息" forState:UIControlStateNormal];
        self.avtarIamgeView.hidden = NO;
        [self.avtarIamgeView sd_setImageWithURL:[NSURL URLWithString:self.personModel.avatar] placeholderImage:[UIImage imageNamed:@""]];
        self.pointLabel.text = [NSString stringWithFormat:@"%.2lf",self.personModel.viewPoint];
        [self.certificationBt setTitle:self.personModel.appraisal?@"已认证" :@"实名认证>" forState:UIControlStateNormal];
        GRBasisKeyModel *basisModel = [GRUserBasisInfo UserBasis];
        basisModel.appraisal = self.personModel.appraisal;
        basisModel.recommendStore = self.personModel.recommendStore;
        basisModel.recommendUser = self.personModel.recommendUser;
        [[GRUserBasisInfo shareBasis] saveBasisKey:basisModel];
        
        if (!self.personModel.recommendSwitch) {
            self.recommendView.hidden = YES;
        }else{
            self.recommendView.hidden = NO;
        }
    self.leftOffset.constant = 81;
        
    }else{
        self.userName.text = @"个人中心";
        [self.loginBt setTitle:@"立即登录" forState:UIControlStateNormal];
        self.leftOffset.constant = 25;
        self.avtarIamgeView.hidden = YES;
        self.pointLabel.text = @"0.00";
        [self.certificationBt setTitle:@"实名认证>" forState:UIControlStateNormal];
        
    }


}

#pragma UICollectionViewDelegate / UICollectinoViewDatasource / UICollectionViewDelegateFlowLayout

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GROwnCenterCell * ownCell = [tableView dequeueReusableCellWithIdentifier:@"ownCell" forIndexPath:indexPath];
    ownCell.titleLabel.text = self.dataSourceArr[indexPath.row];
    return ownCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController * aimVC ;
    if(indexPath.row == 2){
        [NSObject openSystemURL:@"telprompt://4006609727"];
        
    }else{
    switch (indexPath.row) {
        case 0:
        {
            aimVC = [[GRCouponsTableViewController alloc]init];
        }
            break;
        case 1:{
            aimVC = [[GRMyPaidOrderViewController alloc]init];
            
        }
            break;

        default:
            break;
    }
        
        [self.navigationController pushViewController:aimVC animated:YES];
    }
    
    
}

- (IBAction)inviteFriendAction:(id)sender {
    [self haveLoginOperation:^{
        GRInviteFriendViewController * inviteFriendVC = [[GRInviteFriendViewController alloc]init];

        
        [self.navigationController pushViewController:inviteFriendVC animated:YES];
    }];
 
    
}

- (IBAction)myWallletAction:(id)sender {
    [self haveLoginOperation:^{
        GRMyWalletViewController * walletVC = [[GRMyWalletViewController alloc]init];

       
        [self.navigationController pushViewController:walletVC animated:YES];
    }];

}


- (IBAction)loginAction:(id)sender {
    [self haveLoginOperation:^{
        GRMyInfomationViewController *info = [[GRMyInfomationViewController alloc] initWithStyle:UITableViewStylePlain];
        [self.navigationController pushViewController:info animated:YES];
    }];
   
}

- (IBAction)IDCertificationAction:(id)sender {
      [self haveLoginOperation:^{
      if (!self.personModel.appraisal) {
          [self.certificationView show];
      }
      }];

}

-(void)goCertificationAction{
    
            GRIDCardCertificationViewController *idcardVc = [[GRIDCardCertificationViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:idcardVc animated:YES];
    
}


- (IBAction)pointViewAction:(id)sender {
    [self  haveLoginOperation:^{
        GRMyGRiceViewController *griceVc = [[GRMyGRiceViewController alloc] initWithStyle:UITableViewStylePlain];
        [self.navigationController pushViewController:griceVc animated:YES];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
